package com.example.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "exchange_rate_db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_EXCHANGE_RATES = "exchange_rates";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_CURRENCY = "currency";
    public static final String COLUMN_RATE = "rate";
    public static final String COLUMN_TIMESTAMP = "timestamp";

    public static final String TABLE_UPDATE_INFO = "update_info";
    public static final String COLUMN_UPDATE_DATE = "update_date";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_EXCHANGE_RATES_TABLE = "CREATE TABLE " + TABLE_EXCHANGE_RATES + " ("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COLUMN_CURRENCY + " TEXT, "
                + COLUMN_RATE + " TEXT, "
                + COLUMN_TIMESTAMP + " TEXT)";
        db.execSQL(CREATE_EXCHANGE_RATES_TABLE);

        String CREATE_UPDATE_INFO_TABLE = "CREATE TABLE " + TABLE_UPDATE_INFO + " ("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COLUMN_UPDATE_DATE + " TEXT)";
        db.execSQL(CREATE_UPDATE_INFO_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EXCHANGE_RATES);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_UPDATE_INFO);
        onCreate(db);
    }

    public void addRate(SQLiteDatabase db, String currency, String rate, String timestamp) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_CURRENCY, currency);
        values.put(COLUMN_RATE, rate);
        values.put(COLUMN_TIMESTAMP, timestamp);
        db.insert(TABLE_EXCHANGE_RATES, null, values);
    }

    public void deleteAllRates(SQLiteDatabase db) {
        db.delete(TABLE_EXCHANGE_RATES, null, null);
    }

    public List<ExchangeRate> getAllExchangeRates(SQLiteDatabase db) {
        List<ExchangeRate> rates = new ArrayList<>();
        Cursor cursor = db.query(TABLE_EXCHANGE_RATES, null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String currency = cursor.getString(cursor.getColumnIndex(COLUMN_CURRENCY));
                String rate = cursor.getString(cursor.getColumnIndex(COLUMN_RATE));
                String timestamp = cursor.getString(cursor.getColumnIndex(COLUMN_TIMESTAMP));
                rates.add(new ExchangeRate(currency, rate, timestamp));
            }
            cursor.close();
        }
        return rates;
    }

    public String getLastUpdateDate(SQLiteDatabase db) {
        String query = "SELECT " + COLUMN_UPDATE_DATE + " FROM " + TABLE_UPDATE_INFO + " ORDER BY " + COLUMN_ID + " DESC LIMIT 1";
        Cursor cursor = db.rawQuery(query, null);
        String date = null;
        if (cursor != null && cursor.moveToFirst()) {
            date = cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_DATE));
            cursor.close();
        }
        return date != null ? date : ""; // Return empty string if no data
    }

    public void updateLastUpdateDate(SQLiteDatabase db, String currentDate) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_UPDATE_DATE, currentDate);
        db.delete(TABLE_UPDATE_INFO, null, null);
        db.insert(TABLE_UPDATE_INFO, null, values);
    }

    public void addExchangeRates(SQLiteDatabase db, List<ExchangeRate> exchangeRates) {
        // 遍历所有汇率数据并插入到数据库中
        for (ExchangeRate rate : exchangeRates) {
            ContentValues values = new ContentValues();
            values.put(COLUMN_CURRENCY, rate.getCurrency());
            values.put(COLUMN_RATE, rate.getRate());
            values.put(COLUMN_TIMESTAMP, rate.getTimestamp()); // 添加时间戳
            db.insert(TABLE_EXCHANGE_RATES, null, values);
        }
    }
}
